<?php
namespace Admin\Controller;
use Think\Controller;

class QuestioneditController extends Controller {
    public function index(){

    	$qid=I('get.qid');

    	$question = M('question t1')->Field('t1.title,t1.level,t1.content,t1.title,t1.qid,t1.cid,t2.cname')->join('left join category t2 on t1.cid=t2.cid')->find($qid);
    	$category=M('category')->select();

		$newcate=array();

		foreach ($category as $v) {
			if($v['fid']==0){
				$newcate[]=$v;
			}
		}
		foreach ($newcate as $k => $v) {
			foreach ($category as $key => $value) {
				if($value['fid']==$v['cid']){
					$newcate[$k]['son'][]=$value;
				}
			}
		}
		$this->assign('question',$question);

		$this->assign('newcate',$newcate);
        
        $this->display();
    }

    public function edit(){
    	$data = I('post.');/*
    	var_dump($data);exit;*/

    	$res = M('question')->where(['qid'=>$data['qid']])->save($data);

    	if($res){
    		$this->ajaxReturn(['error'=>0,'info'=>'修改成功']);
    	}else{
    		$this->ajaxReturn(['error'=>1,'info'=>'修改失败']);
    	}
    }

    public function upload(){
		//var_dump($_FILES);
		$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/pic/admin'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();
	    if(!$info) {// 上传错误提示错误信息
	        //$this->error($upload->getError());
	        echo "失败";
	    }else{// 上传成功
	        $pic = __ROOT__.'/Uploads/pic/admin'.$info['file']['savepath'].$info['file']['savename'];

	        $arr=array(
	        	"code"=> 0,
	        	"msg"=> '成功',
	        	'data'=>array(
	        		'src'=>$pic,
	        		'title'=>'图片'
	        	)
	        );

	        echo json_encode($arr);
	    }
	}
}

?>